package com.ruoyi.dl.controller;

import java.util.List;

import com.ruoyi.dl.domain.DlUser;
import com.ruoyi.dl.service.IDlUserService;
import com.ruoyi.dl.uitls.Tool;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.dl.domain.DlOrdersGoods;
import com.ruoyi.dl.service.IDlOrdersGoodsService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 发单商品Controller
 * 
 * @author ruoyi
 * @date 2020-12-28
 */
@Controller
@RequestMapping("/dl/goods")
public class DlOrdersGoodsController extends BaseController
{
    private String prefix = "dl/goods";

    @Autowired
    private IDlOrdersGoodsService dlOrdersGoodsService;
    @Autowired
    private IDlUserService dlUserService;

    @RequiresPermissions("dl:goods:view")
    @GetMapping()
    public String goods()
    {
        return prefix + "/goods";
    }

    /**
     * 查询发单商品列表
     */
    @RequiresPermissions("dl:goods:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(DlOrdersGoods dlOrdersGoods)
    {
        startPage();
        List<DlOrdersGoods> list = dlOrdersGoodsService.newSelectDlOrdersGoodsList(dlOrdersGoods);
        return getDataTable(list);
    }

    /**
     * 导出发单商品列表
     */
    @RequiresPermissions("dl:goods:export")
    @Log(title = "发单商品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(DlOrdersGoods dlOrdersGoods)
    {
        List<DlOrdersGoods> list = dlOrdersGoodsService.selectDlOrdersGoodsList(dlOrdersGoods);
        ExcelUtil<DlOrdersGoods> util = new ExcelUtil<DlOrdersGoods>(DlOrdersGoods.class);
        return util.exportExcel(list, "goods");
    }

    /**
     * 新增发单商品
     */
    @GetMapping("/add")
    public String add(ModelMap modelMap)
    {
        List<DlUser> userList=dlUserService.selectDlUserList(new DlUser());
        List<DlOrdersGoods> ordersGoodsList=dlOrdersGoodsService.selectDlOrdersGoodsList(new DlOrdersGoods());
        if(userList!=null){
            modelMap.addAttribute("userList",userList);
        }
        if(ordersGoodsList!=null){
            modelMap.addAttribute("ordersGoodsList",ordersGoodsList);
        }
        return prefix + "/add";
    }

    /**
     * 新增保存发单商品
     */
    @RequiresPermissions("dl:goods:add")
    @Log(title = "发单商品", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(DlOrdersGoods dlOrdersGoods)
    {
        dlOrdersGoods.setGoodsNumber(Tool.generateUniqueKey("FD"));
        return toAjax(dlOrdersGoodsService.insertDlOrdersGoods(dlOrdersGoods));
    }

    /**
     * 修改发单商品
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        DlOrdersGoods dlOrdersGoods = dlOrdersGoodsService.selectDlOrdersGoodsById(id);
        List<DlUser> userList=dlUserService.selectDlUserList(new DlUser());
        List<DlOrdersGoods> ordersGoodsList=dlOrdersGoodsService.selectDlOrdersGoodsList(new DlOrdersGoods());
        if(userList!=null){
            mmap.addAttribute("userList",userList);
        }
        if(ordersGoodsList!=null){
            mmap.addAttribute("ordersGoodsList",ordersGoodsList);
        }
        mmap.put("dlOrdersGoods", dlOrdersGoods);
        return prefix + "/edit";
    }

    /**
     * 修改保存发单商品
     */
    @RequiresPermissions("dl:goods:edit")
    @Log(title = "发单商品", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(DlOrdersGoods dlOrdersGoods)
    {
        return toAjax(dlOrdersGoodsService.updateDlOrdersGoods(dlOrdersGoods));
    }

    /**
     * 删除发单商品
     */
    @RequiresPermissions("dl:goods:remove")
    @Log(title = "发单商品", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(dlOrdersGoodsService.deleteDlOrdersGoodsByIds(ids));
    }
}
